/***************************************************************************
                          sqlite3stmt.cpp  -  description
                             -------------------
    begin                : Sat May 10 2008
    copyright            : (C) 2008 by Samuel Harrington
    email                : robotsam@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "sqlite3stmt.h"

sqlite3Stmt::sqlite3Stmt( sqlite3 *s, const char *t, int ts, const char **tail ) : sql(s) {
  if((last_ret=sqlite3_prepare_v2(sql,t,ts,&stmt,tail))) {
    qWarning("sqlite3Stmt: Could not prepare query(%s): %s",t,sqlite3_errmsg(sql));
    return;
  }
  step();  // Do the first iteration
}

sqlite3Stmt::~sqlite3Stmt() {
  sqlite3_finalize(stmt);
}

/** Go to next row. */
int sqlite3Stmt::step() {
  last_ret=sqlite3_step(stmt);
  if( last_ret != SQLITE_DONE && last_ret != SQLITE_ROW )
    qWarning("sqlite3Stmt: Could not get row: %s",sqlite3_errmsg(sql));
  return last_ret;
}

/** There are no more rows to get. */
bool sqlite3Stmt::done() const {
  return last_ret != SQLITE_ROW;
}

/** Retreive column i as text from the current row. */
const char* sqlite3Stmt::column_text(int i) const {
  return (const char*)sqlite3_column_text(stmt,i);
}

/** Retreive column i as an integer from the current row. */
int sqlite3Stmt::column_int(int i) const {
  return sqlite3_column_int(stmt,i);
}
